/**
 * @Package com.fr.audit.api.util
 * @author jiachy
 * @date 2021/4/6 10:10
 * @version V1.0
 */

package com.inspur.smartclosing.generalledger.utils;

import com.inspur.edp.caf.db.dbaccess.DynamicResultRow;
import com.inspur.edp.caf.db.dbaccess.IDbParameter;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import com.inspur.edp.qdp.bql.api.OptionType;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

@Slf4j
public class Dbservice {
    private IBqlExecuter bqlEx;
    private void getBqlEx() {
        if (bqlEx == null) {
            bqlEx = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);
        }
    }

    public List<DynamicResultRow> getDataListBysql(String sql) {
        try {
            getBqlEx();
            bqlEx.getOptions().setOptionType(OptionType.ExecuteSql);
            return bqlEx.executeSelectStatement(sql);
        } catch (Exception ex) {
            log.error("getDataListBysql.执行sql异常："+sql, ex);
            throw new RuntimeException(ex);
        }
    }

    public List<DynamicResultRow> getDataListByPreparedSql(String sql, Map<String, String> params) {
        try {
            getBqlEx();
            bqlEx.getOptions().setOptionType(OptionType.ExecuteSql);
            IDbParameter[] iDbParameters = new IDbParameter[params.size()];
            int i=0;
            for (Map.Entry<String, String> entry :params.entrySet()){
                iDbParameters[i]=bqlEx.makeInParam(entry.getKey(), entry.getValue());
                i++;
            }
            return bqlEx.executeSelectStatement(sql, iDbParameters);
        } catch (Exception ex) {
            log.error("getDataListByPreparedSql.执行sql异常："+sql, ex);
            throw new RuntimeException(ex);
        }
    }
    public int getDatacountBysql(String sql) {
        try {
            getBqlEx();
            bqlEx.getOptions().setOptionType(OptionType.ExecuteSql);
            return Integer.parseInt(String.valueOf(bqlEx.executeScalar(sql)));
        } catch (Exception ex) {
            log.error("getDatacountBysql.执行sql异常："+sql, ex);
            throw new RuntimeException(ex);
        }
    }
    public int getDataCountByPreparedSql(String sql, Map<String, String> params) {
        try {
            getBqlEx();
            bqlEx.getOptions().setOptionType(OptionType.ExecuteSql);
            IDbParameter[] iDbParameters = new IDbParameter[params.size()];
            int i=0;
            for (Map.Entry<String, String> entry :params.entrySet()){
                iDbParameters[i]=bqlEx.makeInParam(entry.getKey(), entry.getValue());
                i++;
            }
            return Integer.parseInt(String.valueOf(bqlEx.executeScalar(sql, iDbParameters)));
        } catch (Exception ex) {
            log.error("getDatacountByPreparedSql.执行sql异常："+sql, ex);
            throw new RuntimeException(ex);
        }
    }
    public void executeBySql(String sql){
        try {
            getBqlEx();
            bqlEx.getOptions().setOptionType(OptionType.ExecuteSql);
            int i= bqlEx.executeBqlStatement(sql);
        } catch (Exception ex) {
            log.error("executeBySql.执行sql异常："+sql, ex);
        }
    }
    public void executeBySql01(String sql){
        getBqlEx();
        bqlEx.getOptions().setOptionType(OptionType.ExecuteSql);
        int i= bqlEx.executeBqlStatement(sql);
    }
    public void executeByPreparedSql(String sql, Map<String, String> params){
        try {
            getBqlEx();
            bqlEx.getOptions().setOptionType(OptionType.ExecuteSql);
            IDbParameter[] iDbParameters = new IDbParameter[params.size()];
            int i=0;
            for (Map.Entry<String, String> entry :params.entrySet()){
                iDbParameters[i]=bqlEx.makeInParam(entry.getKey(), entry.getValue());
                i++;
            }
            bqlEx.executeBqlStatement(sql, iDbParameters);
        } catch (Exception ex) {
            log.error("executeByPreparedSql.执行sql异常："+sql, ex);
            throw new RuntimeException(ex);
        }
    }

    public boolean insertbatch(List<String> sqls){
        for (String sql:sqls) {
            try {
                getBqlEx();
                bqlEx.getOptions().setOptionType(OptionType.ExecuteSql);
                int i= bqlEx.executeBqlStatement(sql);
            } catch (Exception ex) {
                log.error("Insertbatch.执行sql异常："+sql, ex);
            }
        }
        return true;
    }
}
